/**
 * Filename: NativePreviewActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/29 13:58
 */
package com.github.postsanf.yinian.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.NativePreviewAdapter;
import com.github.postsanf.yinian.bean.YNNativeImage;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页本地预览所有图片
 */
public class NativePreviewActivity extends BaseActivity {
    private Toolbar mToolbar;
    private ViewPager vp_preview_image;                       // 整个切换视图
    private NativePreviewAdapter mAdapter;                               // 图片适配器
    private List<YNNativeImage> picUrlsList = new ArrayList<>();        // 图片数据
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_native_preview_image);
        try {
            position = getIntent().getExtras().getInt(CommonConstants.YNDATAS, 0);
        } catch (NullPointerException e) {
            position = 0;
        }
        initData();
        initView();
        setData();
    }

    private void initData() {
        picUrlsList = YNSingleton.getInstance().getNativePhoto();
        if (picUrlsList.size() == 0) {
            finish();
        }
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.native_preview_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);
        vp_preview_image = (ViewPager) findViewById(R.id.vp_native_preview_image);

    }

    private void setData() {
        mAdapter = new NativePreviewAdapter(this, picUrlsList);
        vp_preview_image.setAdapter(mAdapter);
        final int size = picUrlsList.size();
        if (size > 1) {
            mToolbar.setTitle(1 + "/" + size);
        } else {
            mToolbar.setTitle("预览");
        }

        vp_preview_image.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
                int index = arg0 % size;
                mToolbar.setTitle((index + 1) + "/" + size);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        vp_preview_image.setCurrentItem(position <= size ? position : size);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
